package org.jetnuke.modules.download;

import org.apache.log4j.Logger;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.DownloadDownloadDAO;
import org.jetnuke.entities.DownloadsCategories;

public class ViewDownload implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ViewDownload.class);

	int cid;

	int start;

	String orderby;

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		int limit = 10;

		boolean asc = true;
		if (orderby == null || orderby.length() == 0)
			orderby = "titleA";
		if (orderby.endsWith("D")) {
			asc = false;
		}
		vc.put("cats", DataAccessDriver.getInst().newDownloadCategoriesDAO()
				.selectByParent(cid));
		DownloadDownloadDAO ddm = DataAccessDriver.getInst()
				.newDownloadDownloadDAO();
		// vc.put("cats", downloads_categoriesPeer.doSelect(new Criteria().add(
		// downloads_categoriesPeer.PARENTID, cid)));

		// String column = null;
		List ls = null;
		if (orderby.startsWith("title")) {
			if (asc)
				ls = ddm.selectTitleAsc(cid, start, limit);
			else
				ls = ddm.selectTitleDesc(cid, start, limit);
			// column = downloads_downloadsPeer.TITLE;
		} else if (orderby.startsWith("date")) {
			if (asc)
				ls = ddm.selectDateAsc(cid, start, limit);
			else
				ls = ddm.selectDateDesc(cid, start, limit);
			// column = downloads_downloadsPeer.DATE;
		} else if (orderby.startsWith("rating")) {
			if (asc)
				ls = ddm.selectRatingAsc(cid, start, limit);
			else
				ls = ddm.selectRatingDesc(cid, start, limit);
			// column = downloads_downloadsPeer.DOWNLOADRATINGSUMMARY;
		} else if (orderby.startsWith("hits")) {
			if (asc)
				ls = ddm.selectHitsAsc(cid, start, limit);
			else
				ls = ddm.selectHitsDesc(cid, start, limit);
			// column = downloads_downloadsPeer.HITS;
		}
		// Criteria crt = new Criteria().add(downloads_downloadsPeer.CID, cid)
		// .setOffset(start);
		// if (asc) {
		// crt = crt.addAscendingOrderByColumn(column);
		// } else {
		// crt = crt.addDescendingOrderByColumn(column);
		// }
		// crt = crt.setLimit(limit);
		vc.put("downs", ls);// downloads_downloadsPeer.doSelect(crt));
		LinkedList ls1 = new LinkedList();
		getParentLink(cid, ls1);
		vc.put("cat_downs", ls1);
		vc.put("ccid", new Integer(cid));

		vc.put("per_page", new Integer(limit));
		vc.put("start", new Integer(start));

		vc.put("totalCount", new Integer(ddm.countAllByCategory(cid)));
		// vc.put("totalCount", new Integer(downloads_downloadsPeer
		// .count(new Criteria().add(downloads_downloadsPeer.CID, cid))));

		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

	void getParentLink(int cid1, List downloads) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getParentLink(int, List) - start"); //$NON-NLS-1$
		}

		// DataAccessDriver.getInstance().newDownloadCategoriesModel().retrieveByPK(cid);
		DownloadsCategories cat = DataAccessDriver.getInst()
				.newDownloadCategoriesDAO().retrieveByPK(cid1);
		downloads.add(0, cat);
		if (cat.getPARENTID() > 0) {
			getParentLink(cat.getPARENTID(), downloads);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getParentLink(int, List) - end"); //$NON-NLS-1$
		}
	}

}
